﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;


namespace Topro.Web.Controllers
{
    using System.Linq;
    using System.Text.RegularExpressions;
    using Topro.Application.Interface;
    using Topro.Entity;
    using Topro.Entity.Enum;
    using Topro.Entity.Query;
    using Topro.Entity.Request.InventoryItem;
    using Topro.Entity.Request.StockIn;
    using Topro.Entity.Response.StockIn;
    using Topro.Util;

    /// <summary>
    /// 出入库控制器
    /// </summary>
    public class StockInController : BaseController
    {
        #region 注入
        /// <summary>
        /// 
        /// </summary>
        private readonly IUId _uid;

        /// <summary>
        /// 日志接口
        /// </summary>
        private readonly LogService _logService;

        /// <summary>
        /// 入库接口
        /// </summary>
        private readonly StockInService _stockInService;

        /// <summary>
        /// 库存接口
        /// </summary>
        private readonly InventoryService _inventoryService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logService">日志接口</param>
        /// <param name="stockInService">入库接口</param>
        public StockInController(LogService logService, IUId uid, StockInService stockInService, InventoryService inventoryService)
        {
            _logService = logService;
            _uid = uid;
            _stockInService = stockInService;
            _inventoryService = inventoryService;
        }
        #endregion

        #region 入库
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public new IActionResult Index()
        {
            ViewBag.StockInStatus = (Dictionary<int, string>)EnumUtil.ToDictionary(typeof(StockInStatus));

            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>DataGrid<StockInQueryResponse></returns>
        public JsonResult InventoryQuery(InventoryQuery query)
        {

            query.OrderType = (int)OrderType.入库;
            query.Status = (int)InventoryStatus.待处理;

            var list = _inventoryService.StockInQuery(query);

            return Json(list);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public IActionResult StockInAddOrEdit()
        {
            ViewBag.NextId = _uid.NextId();

            return View("StockInAddOrEdit");
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public JsonResult Save(StockInSaveRequest stockIn, List<InventoryItemSaveRequest> inventoryItem)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                //验证单据编号
                if (string.IsNullOrEmpty(stockIn.OrderId))
                    throw new Exception("订单号不能为空.");
                //托盘编码
                if (!stockIn.ContainerCode.HasValue)
                    throw new Exception("请输入托盘编码.");
                //托盘高度
                if (!stockIn.Type.HasValue)
                    throw new Exception("请输入托盘高度.");
                if(stockIn.Type.Value<=0)
                    throw new Exception("托盘高度参数错误.");
                //站台
                if (!stockIn.StationNo.HasValue)
                    throw new Exception("请选择入库站台.");
                //创建人
                stockIn.CreatedBy = UserId;
                response = _stockInService.Save(stockIn, inventoryItem);

            }
            catch(Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }
        #endregion

        #region 解析
        /// <summary>
        /// 解析
        /// </summary>
        /// <returns></returns>
        public IActionResult Explain(string content)
        {
            var response = new JResult<ExplainResponse>()
            {
                Success = false
            };

            try
            {
                var explainResponse = new ExplainResponse();

                //分割数组
                var arrs = content.Split('@');

                if (arrs.Count() > 3)
                {
                    foreach(var arr in arrs)
                    {
                        #region 料号
                        var partNoReg = new Regex(@"^PN1");
                        if (partNoReg.IsMatch(arr))
                        {
                            explainResponse.PartNo = arr.Replace("PN1","");
                        }

                        var lowerPartNoReg = new Regex(@"^pn1");
                        if (lowerPartNoReg.IsMatch(arr))
                        {
                            explainResponse.PartNo = arr.Replace("pn1", "");
                        }
                        #endregion

                        #region 物料名称
                        var productNameReg = new Regex(@"^TE1");
                        if (productNameReg.IsMatch(arr))
                        {
                            explainResponse.ProductName = arr.Replace("TE1", "");
                        }

                        var ppProductNameReg = new Regex(@"^te1");
                        if (ppProductNameReg.IsMatch(arr))
                        {
                            explainResponse.ProductName = arr.Replace("te1", "");
                        }

                        //规格大写
                        var specsReg = new Regex(@"^SC1");
                        if (specsReg.IsMatch(arr))
                        {
                            explainResponse.ProductName += $"[{arr.Replace("SC1", "")}]";
                        }
                        //规格小写
                        var lowerSpecsReg = new Regex(@"^sc1");
                        if (lowerSpecsReg.IsMatch(arr))
                        {
                            explainResponse.ProductName += $"[{arr.Replace("sc1", "")}]";
                        }
                        #endregion

                        #region 数量 
                        var qtyReg = new Regex(@"^QT1");
                        if (qtyReg.IsMatch(arr))
                        {
                            explainResponse.Qty = arr.Replace("QT1", "");
                        }
                        var lowerQtyReg = new Regex(@"^qt1");
                        if (lowerQtyReg.IsMatch(arr))
                        {
                            explainResponse.Qty = arr.Replace("qt1", "");
                        }
                        #endregion

                        #region 批号 
                        var lotNoReg = new Regex(@"^LN1");
                        if (lotNoReg.IsMatch(arr))
                        {
                            explainResponse.LotNo = arr.Replace("LN1", "");
                        }
                        var lowerLotNoReg = new Regex(@"^ln1");
                        if (lowerLotNoReg.IsMatch(arr))
                        {
                            explainResponse.LotNo = arr.Replace("ln1", "");
                        }
                        #endregion

                        #region 等级
                        var gradeReg = new Regex(@"^LL1");
                        if (gradeReg.IsMatch(arr))
                        {
                            explainResponse.Grade = arr.Replace("LL1", "");
                        }

                        var lowerGradeReg = new Regex(@"^ll1");
                        if (lowerGradeReg.IsMatch(arr))
                        {
                            explainResponse.Grade = arr.Replace("ll1", "");
                        }
                        #endregion
                    }

                    response.Success = true;
                    response.Data = explainResponse;
                }
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }
        #endregion
    }
}
